﻿using System;
using System.Collections.Generic;
using __Able.Core.Utilities;
using __Able.Network;
using __Able.Network.Events;
using __Able.Network.Packets;
using __Able.Network.RequestPacket;

namespace CCU.Main
{
    public class CCU
    {
        /// <summary>
        /// 当前服务器信息
        /// </summary>
        private Dictionary<int, ServersMainInfo> servers;

        public CCU(Dictionary<int, ServersMainInfo> servers)
        {
            this.servers = servers;
        }

        /// <summary>
        /// 开始监听端口
        /// </summary>
        public void BeginReceive()
        {
            //IDictionaryEnumerator emtor = servers.GetEnumerator();
            //while (emtor.MoveNext())
            //{
                //ServersMainInfo server = emtor.Value as ServersMainInfo;

                Channel channel = new Channel();
                channel.Error += new EventHandler<EventArgs<ErrorPacket>>(channel_Error);

                channel.PacketManager.LostConnection += new EventHandler<EventArgs<_OutPacket>>(PacketManager_LostConnection);
                channel.PacketManager.ReceivedUnknownPacket += new EventHandler<EventArgs<UnknownRequestPacket>>(PacketManager_ReceivedUnknownPacket);
                channel.PacketManager.SendPacketTimeOut += new EventHandler<EventArgs<_OutPacket>>(PacketManager_SendPacketTimeOut);
                channel.PacketManager.SentPacketSuccessfully += new EventHandler<EventArgs<_OutPacket, _InPacket>>(PacketManager_SentPacketSuccessfully);

                ConsoleAttributes.ForeColor = __Able.Core.Utilities.ConsoleColor.LightGreen;
                Console.WriteLine(string.Format("[{0}@port:{1}] CCU BeginReceive... ", "CCU", channel.Port));
                ConsoleAttributes.ForeColor = __Able.Core.Utilities.ConsoleColor.Gray;

                channel.ConnectionManager.ConnectServerError += new EventHandler<System.IO.ErrorEventArgs>(ConnectionManager_ConnectServerError);
                channel.ConnectionManager.ConnectSuccessfully += new EventHandler(ConnectionManager_ConnectSuccessfully);
                channel.ConnectionManager.NetworkError += new EventHandler<System.IO.ErrorEventArgs>(ConnectionManager_NetworkError);
            //}
        }

        void ConnectionManager_NetworkError(object sender, System.IO.ErrorEventArgs e)
        {
            Console.WriteLine("ConnectionManager_NetworkError");
        }

        void ConnectionManager_ConnectSuccessfully(object sender, EventArgs e)
        {
            Console.WriteLine("ConnectionManager_ConnectSuccessfully");
        }

        void ConnectionManager_ConnectServerError(object sender, System.IO.ErrorEventArgs e)
        {
            Console.WriteLine("ConnectionManager_ConnectServerError");
        }
        void PacketManager_SentPacketSuccessfully(object sender, EventArgs<_OutPacket, _InPacket> e)
        {
            Console.WriteLine("PacketManager_SentPacketSuccessfully");
        }
        void PacketManager_SendPacketTimeOut(object sender, EventArgs<_OutPacket> e)
        {
            Console.WriteLine("PacketManager_SendPacketTimeOut");
        }
        void PacketManager_ReceivedUnknownPacket(object sender, EventArgs<UnknownRequestPacket> e)
        {
            Console.WriteLine("PacketManager_ReceivedUnknownPacket");
        }
        void PacketManager_LostConnection(object sender, EventArgs<_OutPacket> e)
        {
            Console.WriteLine("PacketManager_LostConnection");
        }
        void channel_Error(object sender, EventArgs<ErrorPacket> e)
        {
            Console.WriteLine("channel_Error");
        }
    }
}
